package pt.fct.mpc.lastfm.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class LastfmDatabaseSearchHistory
	extends LastfmDatabaseAdapter
{

	public LastfmDatabaseSearchHistory(Context context)
	{
		super(context);
	}
	
	private void addEntry(String search_query, String search_type, String extra)
	{
		SQLiteDatabase conn = super.getConn();
		
		String[] cols = new String[] { 
			LASTFM_TABLE_SEARCH_HISTORY_COL_TEXT, 
			"hits", 
			LASTFM_TABLE_SEARCH_HISTORY_COL_TYPE,
			LASTFM_TABLE_SEARCH_HISTORY_COL_INTENT,
			LASTFM_TABLE_SEARCH_HISTORY_COL_INTENT_EXTRA,
		};
		String select = LASTFM_TABLE_SEARCH_HISTORY_COL_TEXT+"='"+search_query
		+"' and "+LASTFM_TABLE_SEARCH_HISTORY_COL_TYPE+"='"+search_type+"'";
		
		conn.beginTransaction();
		
		Cursor cursor = conn.query(LASTFM_TABLE_SEARCH_HISTORY, 
			cols, select, null, null, null, null);
		
		int row_count = cursor.getCount();
		if (row_count > 1)
			throw new SQLiteException("we have more than one hit on DB");
		
		if (row_count == 0)
		{
			// insert
			ContentValues values = new ContentValues();
			values.put(LASTFM_TABLE_SEARCH_HISTORY_COL_TEXT, search_query);
			values.put("hits", 1);
			values.put(LASTFM_TABLE_SEARCH_HISTORY_COL_TYPE, search_type);
			values.put(LASTFM_TABLE_SEARCH_HISTORY_COL_INTENT, search_query);
			
			String intent_extra = search_query + ":::" + search_type;
			if (extra != null) // for the album's artist name
				intent_extra = intent_extra + ":::" + extra;
			values.put(LASTFM_TABLE_SEARCH_HISTORY_COL_INTENT_EXTRA, intent_extra);
			
			long insert = conn.insert(LASTFM_TABLE_SEARCH_HISTORY, null, values);
			if (insert < 0)
				throw new SQLiteException("unable to insert row");
		} else
		{
			// update row's hits
			
			cursor.moveToFirst();
			
			int hits_column_index = cursor.getColumnIndexOrThrow("hits");
			int hits_count = cursor.getInt(hits_column_index) + 1;
			
			ContentValues values = new ContentValues();
			values.put("hits", hits_count);
			
			String whereClause =
				LASTFM_TABLE_SEARCH_HISTORY_COL_TEXT+"='"+search_query+"'"
				+"and "
				+LASTFM_TABLE_SEARCH_HISTORY_COL_TYPE+"='"+search_type+"'";
			int updates = conn.update(LASTFM_TABLE_SEARCH_HISTORY, values, 
				whereClause, null);

			if (updates <= 0)
				throw new SQLiteException("unable to update values");
		}
		
		cursor.close();
		
		conn.setTransactionSuccessful();
		conn.endTransaction();
	}
	
	public void addArtist(String search_query)
	{
		addEntry(search_query, LASTFM_DATABASE_TYPE_ARTIST, null);
	}
	
	public void addAlbum(String search_query, String extra)
	{
		addEntry(search_query, LASTFM_DATABASE_TYPE_ALBUM, extra);
	}
	
	public class SearchHistoryResult
	{
		private int type;
		private int hits;
		private String name;

		public SearchHistoryResult(String name, int hits, int type)
		{
			this.name = name;
			this.hits = hits;
			this.type = type;
		}

		public String getName() 
		{
			return this.name;
		}
		
		public int getType()
		{
			return this.type;
		}
		
		public int getHits()
		{
			return this.hits;
		}
	}
}
